package com.codefish.codefish_blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * spring内置定时任务配置类
 *
 * @author codefish
 * @version 1.0
 * @date 2022/04/15 下午 08:45
 */
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(taskThreadPool());
    }

    /**
     * 指定定时任务使用的线程池
     *
     * @return scheduledThreadPool
     */
    @Bean(destroyMethod = "shutdown")
    public ExecutorService taskThreadPool() {
        return Executors.newScheduledThreadPool(5);
    }
}
